package com.gc.util;


import com.gc.controller.LoginServlet;
import com.gc.entity.Reader;
import com.gc.repository.impl.ReaderRepositoryImpl;
import com.gc.service.LoginService;
import com.gc.service.impl.LoginServiceImpl;
import com.mchange.v2.c3p0.ComboPooledDataSource;



import javax.sql.DataSource;
import java.sql.*;

public class JDBCTools {
    private static DataSource dataSource;
    static  {
        dataSource = new ComboPooledDataSource("testc3p0");
    }

    public static Connection getConnection() {
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }


        return connection;
    }

    public static void release(Connection connection, Statement statement, ResultSet resultSet) {
        try {
            if(connection != null) {
                connection.close();
            }
            if(statement != null) {
                statement.close();
            }
            if(resultSet != null) {
                resultSet.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        LoginService loginService = new LoginServiceImpl();
        Reader reader = loginService.login("lisi", "123123");
        System.out.println(reader);
    }
}
